import { login, reqGetUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { constantRoutes, resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {},
    routes: constantRoutes
  },
  mutations: {
    setToken(state, payload) {
      state.token = payload
      setToken(payload)
    },
    setInfo(state, payload) {
      state.userInfo = payload
    },
    logout(state) {
      state.token = ''
      state.userInfo = {}
      removeToken()
      // 重置路由

      resetRouter()
      state.routes = constantRoutes
    },
    addRoute(state, routes) {
      state.routes = [...routes, ...constantRoutes]
    }
  },
  actions: {
    async login({ commit }, payload) {
      const { data } = await login(payload)
      console.log(data)
      commit('setToken', data)
    },
    async getInfo({ commit }) {
      const { data } = await reqGetUserInfo()
      // console.log(data)
      commit('setInfo', data)
    }
  }
}
